package edu.columbia.cloudbox.policy;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import android.content.Context;
import android.util.Log;
import edu.columbia.cloudbox.UserStatusService;

public class UserPreference implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2162373469667356745L;

	private static final String className = UserPreference.class
			.getSimpleName();

	public static boolean isManual = true;

	public static List<String> userExts = new Vector<String>();

	public static int userPolicy = 0;

	public static long userInterval = 0;;

	public static final String FILE_NAME = "preference.properties";

	public static final String IS_MANUAL = "is.manual";

	public static final String USER_EXTS = "user.exts";

	public static final String USER_AUTO_POLICY = "user.auto.policy";

	public static final String USER_CACHE_INTERVAL = "user.interval";

	private static final UserPreference _instance = new UserPreference();

	private UserPreference() {

	}

	public static UserPreference getInstance() {
		return _instance;
	}

	public void savePreference(Context context) {
		final String methodName = "save()";
		Log.i(className, methodName + ": Entering...");
		Properties properties = new Properties();
		properties.setProperty(IS_MANUAL, String.valueOf(isManual));
		Log.i(className,
				methodName + ": " + IS_MANUAL + "=" + String.valueOf(isManual));

		StringBuilder extsPropertyBuilder = new StringBuilder();
		for (String userExt : userExts) {
			extsPropertyBuilder.append(userExt + ",");
		}
		String extsProperty = extsPropertyBuilder.toString();
		extsProperty = extsProperty.substring(0, extsProperty.length() - 1);
		properties.setProperty(USER_EXTS, extsProperty);

		properties.setProperty(USER_AUTO_POLICY, String.valueOf(userPolicy));

		properties.setProperty(USER_CACHE_INTERVAL,
				String.valueOf(userInterval));

		Log.i(className, methodName + ": " + USER_EXTS + "=" + extsProperty);
		
		try {
			properties.store(UserStatusService.LocalIO_INSTANCE
					.getOutputStream(FILE_NAME), "User Preference");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		UserStatusService.initialize(context);
	}

	public void loadPreference() {
		final String methodName = "load()";
		Log.i(className, methodName + ": Entering...");
		Properties properties = new Properties();
		InputStream fis = UserStatusService.LocalIO_INSTANCE
				.getInputStream(FILE_NAME);
		if (fis == null) {
			Log.i(className, methodName + ": " + FILE_NAME
					+ " doesn't exist! Set default!");
			isManual = true;
			userExts.clear();
			return;
		}

		try {
			Log.i(className, methodName + ": Loading " + FILE_NAME);
			properties.load(fis);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.i(className, methodName + ": Loading " + IS_MANUAL);
		isManual = Boolean.valueOf(properties.getProperty(IS_MANUAL));

		Log.i(className, methodName + ": Loading " + USER_EXTS);
		String extsProperty = properties.getProperty(USER_EXTS);
		String[] exts = extsProperty.split(",");
		userExts.clear();
		for (String ext : exts) {
			userExts.add(ext);
		}

		userInterval = Long.valueOf(properties.getProperty(USER_CACHE_INTERVAL,
				"0"));

		userPolicy = Integer.valueOf(properties.getProperty(USER_AUTO_POLICY,
				"0"));

	}
}
